package com.dayuanit.dy17.atm.util;

import com.dayuanit.dy17.atm.config.AtmPropertiesConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SignUtils {

    @Autowired
    private AtmPropertiesConfig atmPropertiesConfig;

    public String createLoginPasswordSign(String password, String username) {
        //对明文进行混淆 也就是加盐操作 防止彩虹表进行破解
        String passwordTxt = password + atmPropertiesConfig.getUserPasswordSalt() + username;
        return DigestUtils.md5Hex(passwordTxt);
    }

    public boolean checkLoginPasswordSign(String username, String passsword, String originPasswordSign) {
        String passwordSign = createLoginPasswordSign(passsword, username);
        return originPasswordSign.equals(passwordSign);
    }


    public String createResetPwdSign(String email, long createTime) {
        String msgTxt = email + atmPropertiesConfig.getUserEmailSalt() + createTime;
        return DigestUtils.md5Hex(msgTxt);
    }

    public boolean checkRestPwdSign(String email, String sign, long createTime) {
        String originSign = createResetPwdSign(email, createTime);
        return originSign.equals(sign);
    }

    public String createActiveEmailSign(String email, String username, long createTime) {
        //明文格式=email + 盐值 + 用户名
        String msgTxt = email + atmPropertiesConfig.getUserEmailSalt() + username + createTime;
        return DigestUtils.md5Hex(msgTxt);
    }

    public boolean checkActiveEmailSign(String email, String username, long createTime, String sign) {
        String originSigin = createActiveEmailSign(email, username, createTime);
        return originSigin.equals(sign);
    }
}
